﻿<?php
if(!defined('CONFIG'))
{
    define('CONFIG',__DIR__);
    //获取用户信息
    $user = require_once(CONFIG.'/config.php');
    require_once CONFIG.'/capture.php';
}
print_r('-------任务开始-------');


$curl = new Curl();
$array = array();

foreach ($user as $key => $val){

	foreach ($val['user'] as $k => $v) {

        //鉴权
        $curl->header(array(

            'Referer:https://www.wang1314.com/login/',

            'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',

        ))->get('https://www.wang1314.com/login/');

		$curl->cookie($curl->Cookies)->post('https://www.wang1314.com/login.do', array(

		    'userId' => $v['userName'],

		    'psw' => $v['userPass'],

		    'Submit' => '登录网络收藏夹',

		    'fromUri' => 'https://www.wang1314.com/doc/PrepareForNewTopicAction.do'

		));

		$data = $curl->cookie($curl->Cookies)->get('https://www.wang1314.com/doc/my/');

		$regex4 ='#<div class="title"><a.*?"(?<url>.*?)">(?<title>.*?)<.*?<br />(?<date>.*?)<#';

		preg_match_all($regex4,$data,$arr);
        
		//统计有多少条数据
		$count = count($arr['title']);
		// var_dump($count);exit;

		$array_user = array();

		//获取当天的日期
		$time = date('Y-m-d',time());
		// var_dump($time);exit;

		for ($i=0; $i<$count ; $i++) {
			if ($arr['date'][$i] != $time) {
				break;
			}else{
				$url_model = url_model($arr['url'][$i]);
				$array_user[] = array(
					'title'=>$arr['title'][$i],
					//web端url地址
					'url'=>"https://www.wang1314.com".$arr['url'][$i],
					//移动端url地址
					'urlmodel' =>"https://wang1314.com/doc/webapp/topic/$url_model.html",
					'date'=>$arr['date'][$i],
					);
			}
		}
		if (count($array_user) != 0) {
			array_unshift($array_user,array('user'=>$v['userName']));
		}
		//获取所有用户发送的数据信息
		$array[] = $array_user;
	}
}

$data_res = array();
foreach ($array as $kk=> $vv) {
	if ($vv != array() ){
		$data_res = array_merge($data_res,$vv);
	}
}
//推送百度https和http
foreach ($data_res as $kks => $vvs) {
	if(isset($vvs['url'])){
		$baidu_ress = baidu_push($vvs['url']);
		$baidu_res = baidu_push(str_ireplace('https', 'http', $vvs['url']));
	}
}
//遍历数组为可以插入csv文件的数据
$data_csv = outputCsv($data_res);
//存储进svc文件
svc($data_csv);


print_r("------任务结束------");

//处理数组为需要插入CSV的格式
 function outputCsv($data) {
        $str =  "";
        foreach ($data as $fields) {
            foreach ($fields as $key => $value) {
                $str .= $value . ',';
            }
            $str .= "\r\n";
        }
        return $str;
    }

//写入svc文件
function svc($data_csv){
	$fielName = dirname(__FILE__)."/csv/".date('m-d').".csv";
	file_put_contents($fielName, $data_csv);
}


function baidu_push($url)
{
    // 提交地址
    $pushUrl = 'http://api.share.baidu.com/s.gif?l=' . $url;

    // 追加协议头
    $header = array(
        'Referer'    => $url,
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36'
    );
    // 初始化
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 65);
    curl_setopt($ch, CURLOPT_URL, $pushUrl);
    curl_setopt($ch, CURLOPT_USERAGENT, $header['User-Agent']);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    
    curl_exec($ch);
    $retHtml = curl_getinfo($ch);

    $code = false;
    
    if($retHtml["http_code"] == 200){
        $code = true;
    }
    return $code;
}

//生成移动端url
function url_model($url){
	preg_match_all("#topic-(?<url>.*?)-#", $url, $arr);
	return $arr['url'][0];
}

 ?>